import type { Ref } from 'vue'

export default function useTableSelection<T>(
  dataSource: Ref<T[]>,
  paginationRef?: Ref
) {
  const selectedRows = ref<T[]>()

  const handleTableSelectionChange = (arr: T[]) => {
    selectedRows.value = arr
    const checkAll = selectedRows.value.length === dataSource.value.length
    if (paginationRef) {
      paginationRef.value.selectionChange(
        checkAll,
        selectedRows.value.length > 0 && !checkAll
      )
    }
  }

  return {
    selectedRows,
    handleTableSelectionChange
  }
}
